//
//  MyTradeConfirmTableVC.m
//  SocietySale
//
//  Created by Mike on 16/11/14.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MyTradeConfirmTableVC.h"
#import "MSTradeConfirmHeadView.h"
#import "MSTradeConfirmPayView.h"
#import "MSTradeSuccessCell.h"

@interface MyTradeConfirmTableVC () <UITextFieldDelegate>
@property (nonatomic, strong) MSTradeConfirmHeadView   *tradeConfirmHeadView;
@property (nonatomic, assign) BOOL                     isProjectProcess;
@property (nonatomic, strong) UILabel *tradePriceLabel;
@property (nonatomic, strong) UILabel *memberTypeLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIView  *footerView;

//项目信息数据
@property(nonatomic,copy)NSArray *projectInfoArray;
@end

@implementation MyTradeConfirmTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)setModel:(MSTradeModel *)model{
    _model = model;
    
    NSString *tempStr = @"";
    if ([model.type isEqualToString:@"1"]) {//出售
        tempStr = @"出售";
    }
    else{//求购
        tempStr = @"求购";
    }
    NSString *type = [NSString stringWithFormat:@"%@  %@",tempStr,model.projectType];
    
    if ([model.typeGroup isEqualToString:@"contacts"]){//联系人
        if ([model.type isEqualToString:@"1"]) {//出售
            self.projectInfoArray = @[[DOKeyValue key:@"类型" value:type],
                                      [DOKeyValue key:@"项目" value:model.title],
                                      [DOKeyValue key:@"姓名" value:model.name],
                                      [DOKeyValue key:@"职务" value:model.position],
                                      ];
        }
        else{//求购
            self.projectInfoArray = @[[DOKeyValue key:@"类型" value:type],
                                      [DOKeyValue key:@"项目" value:model.title],
                                      ];
        }
    }
    else{//项目进展
        self.projectInfoArray = @[[DOKeyValue key:@"类型" value:type],
                                  [DOKeyValue key:@"项目" value:model.title],
                                  [DOKeyValue key:@"主题" value:model.theme],
                                  [DOKeyValue key:@"内容" value:model.content],
                                  ];
    }
    
    if ([_model.type isEqualToString:@"2"] && [_model.typeGroup isEqualToString:@"progress"]) {
        _isProjectProcess = YES;
    }
//    [self.tableView reloadData];
}

- (void)setContactArray:(NSMutableArray *)contactArray{
    _contactArray = contactArray;
//    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([_model.type isEqualToString:@"2"]){
        return 2;
    }
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        if ([_model.type isEqualToString:@"2"] && [_model.typeGroup isEqualToString:@"contacts"]) {
            return 6;
        }
        else{
            return 0;
        }
    }
    return self.projectInfoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *identifier = @"InfoCell";
        MSTradeSuccessCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[MSTradeSuccessCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.data = self.projectInfoArray[indexPath.row];
        return cell;
    }
    if (indexPath.section == 1) {
        NSString *cellId = @"cellId";
        MSTradeConfirmContactsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[MSTradeConfirmContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (indexPath.row == self.contactArray.count-1) {
            cell.horizonLineView.hidden = YES;
        }
        else{
            cell.horizonLineView.hidden = NO;
        }
        cell.delegate = (id)self.delegate;
        cell.inputField.delegate = self;
        cell.inputField.tag = indexPath.row;
        cell.data = self.contactArray[indexPath.row];
        return cell;
    }
    return nil;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    DOKeyValue *data = self.contactArray[textField.tag];
    data.value = textField.text;
    return YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return [MSTradeSuccessCell cellHeightWithModel:self.projectInfoArray[indexPath.row]];
    }
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return  0.01;
            break;
        case 1:
            if (_isProjectProcess){
                return  131.2;
            }
            return  0.01;
            break;
        default:
            break;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return  nil;
            break;
        case 1:
            if (_isProjectProcess){
                UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 150)];
                bgView.backgroundColor = [UIColor whiteColor];
                
                _textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH-24, 40)];
                _textField.placeholder = @"请填写项目进展";
                _textField.textColor = [UIColor blackColor];
                _textField.font = HeitiFont(14);
                [bgView addSubview:_textField];
                
                UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 0.6)];
                horizonLineView.backgroundColor = kNormalGray;
                [bgView addSubview:horizonLineView];
                
                self.remarkTextView = [[SXTextView alloc] initWithFrame:CGRectMake(7, horizonLineView.bottom, SCREEN_WIDTH-10, 90)];
                self.remarkTextView.font = HeitiFont(14);
                self.remarkTextView.placeholder = @"请填写内容描述";
                [bgView addSubview:self.remarkTextView];
                
                UIView *horizonLineView2 =[[UIView alloc] initWithFrame:CGRectMake(12, self.remarkTextView.bottom, SCREEN_WIDTH-24, 0.6)];
                horizonLineView2.backgroundColor = kNormalGray;
                [bgView addSubview:horizonLineView2];
                
                return bgView;
            }

            return  nil;
            break;
        default:
            break;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        if ([_model.type isEqualToString:@"1"]) {//出售
            return 40;
        }
        return 50;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50)];
        bgView.backgroundColor = kDefaultViewBackgroundColor;
        
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        _footerView.backgroundColor = [UIColor whiteColor];
        [bgView addSubview:_footerView];
        
        UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 2, SCREEN_WIDTH-24, 0.6)];
        horizonLineView.backgroundColor = kNormalGray;
        [_footerView addSubview:horizonLineView];
        
        self.tradePriceLabel = [self creatLabel:CGRectMake(12, 12, SCREEN_WIDTH/2, 20)];
        self.memberTypeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH/2-20, self.tradePriceLabel.top, 130, 20)];
        self.timeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH-92, self.tradePriceLabel.top, 80, 20)];
        self.timeLabel.textAlignment = NSTextAlignmentRight;
        
        [self setValueGiveLabelWithModel:_model];
        return bgView;
    }
    return nil;
}

- (void)setValueGiveLabelWithModel:(MSTradeModel *)model{
    CGSize priceSize = [HDUtil getSizeWithText:model.tradePrice font:[UIFont systemFontOfSize:15]];
    self.memberTypeLabel.left = 100+priceSize.width;
    
    NSMutableAttributedString *priceAttstr = [[NSMutableAttributedString alloc] initWithString:@"价格     x " attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.bounds = CGRectMake(0, -4, 17, 17);
    textAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [priceAttstr insertAttributedString:attachmentString atIndex:6];
    
    NSAttributedString *priStr = [[NSAttributedString alloc] initWithString:model.tradePrice attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kLightBlackColor}];
    [priceAttstr appendAttributedString:priStr];
    self.tradePriceLabel.attributedText = priceAttstr;
    
    NSString *memberTypeStr = [NSString stringWithFormat:@"[%@]",model.memberType];
    NSMutableAttributedString *memberAttstr = [[NSMutableAttributedString alloc] initWithString:memberTypeStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    if ([model.memberLevel integerValue] >0) {
        NSTextAttachment *memberAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        memberAttachment.bounds = CGRectMake(0, -2.5, 15, 12.5);
        memberAttachment.image = [UIImage imageNamed:@"diamond"];
        NSAttributedString *memberAttachmentStr = [NSAttributedString attributedStringWithAttachment:memberAttachment];
        
        for (int i = 0; i<[model.memberLevel integerValue]; i++) {
            [memberAttstr insertAttributedString:memberAttachmentStr atIndex:memberTypeStr.length+i];
        }
    }
    
    self.memberTypeLabel.attributedText = memberAttstr;
    
    NSMutableAttributedString *timeAttstr = [[NSMutableAttributedString alloc] initWithString:model.time attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
    textAttachment2.image = [UIImage imageNamed:@"time"];
    NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
    [timeAttstr insertAttributedString:attachmentString2 atIndex:0];
    
    self.timeLabel.attributedText = timeAttstr;
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    [_footerView addSubview:label];
    return label;
}

@end
